package com.itheima.controller;

import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Setmeal;
import com.itheima.resources.MessageConstant;
import com.itheima.resources.QiniuUtils;
import com.itheima.resources.RedisConstant;
import com.itheima.resources.UploadUtils;
import com.itheima.service.SetMealService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.io.IOException;
import java.util.Map;

@RestController
@RequestMapping(value = "/setMeal",name="/套餐模块")
public class SetMealController {

    @Reference
    private SetMealService setMealService;
    @Autowired //注入连接池对象
    private JedisPool jedisPool;

    @RequestMapping(value = "/picUpload",name = "/图片上传功能")
    public Result picUpload(@RequestParam("imgFile") MultipartFile file){
        try {
            //使用自定义工具类,获取uuid文件名
            String uuidFilename = UploadUtils.generateRandonFileName(file.getOriginalFilename());

            //使用自定义的七牛云工具类  上传图片文件 参数1:图片对象的字节数组,参数2:图片对象的文件名
            QiniuUtils.upload2Qiniu(file.getBytes(),uuidFilename);

            //获取一个连接对象
            Jedis jedis = jedisPool.getResource();
            //将文件名作为value值存储到redis的set中去 key值为固定值, 意指存储到七牛云中去了.
            jedis.sadd(RedisConstant.SETMEAL_PIC_RESOURCES,uuidFilename);

            System.out.println("上传到七牛云的图片名已存到redis中");

            //返还连接对象
            jedis.close();

            //上传成功返回对象  顺便将生成的uuid文件名也返回回去
            return new Result(true, MessageConstant.PIC_UPLOAD_SUCCESS,uuidFilename);
        } catch (IOException e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.PIC_UPLOAD_FAIL);
        }
    }

    @RequestMapping(value = "/addSetMeal",name = "/添加套餐")
    public Result addSetMeal(@RequestBody Setmeal setMeal, @RequestParam("checkGroupIds") Integer[] checkGroupIds){
        try {
            //调用生产者添加数据
            setMealService.addSetMeal(setMeal,checkGroupIds);
            //添加成功
            return new Result(true, MessageConstant.ADD_SETMEAL_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            //添加失败
            return new Result(false,MessageConstant.ADD_SETMEAL_FAIL);
        }
    }

    @RequestMapping(value = "/findPageSetMeal",name = "/分页查询")
    public Result findPageSetMeal(@RequestBody QueryPageBean queryPageBean) {
        try {
            //调用生产者查询数据
            Map map = setMealService.findPageSetMeal(queryPageBean);
            //查询成功
            return new Result(true, MessageConstant.QUERY_SETMEAL_SUCCESS, map);
        } catch (Exception e) {
            e.printStackTrace();
            //查询失败
            return new Result(false, MessageConstant.QUERY_SETMEAL_FAIL);
        }
    }

    @RequestMapping(value = "/echoCheckGroupIds.do",name = "/查询当前id值套餐对应的检查组数据")
    public Result findSetMealById(Integer id){
        try {
            //调用生产者查询数据
            Map map = setMealService.echoCheckGroupIds(id);
            //查询成功
            return new Result(true, MessageConstant.QUERY_SETMEAL_SUCCESS,map);
        } catch (Exception e) {
            e.printStackTrace();
            //查询失败
            return new Result(false,MessageConstant.QUERY_SETMEAL_FAIL);
        }
    }

    @RequestMapping(value = "/deleteSetMeal.do",name = "/删除当前id值对应的套餐数据")
    public Result deleteSetMeal(Integer id){
        try {
            //调用生产者删除数据
            setMealService.deleteSetMeal(id);
            //删除成功
            return new Result(true, "删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            //删除失败
            return new Result(false,"删除失败");
        }
    }

    @RequestMapping(value = "/updateSetMeal.do",name = "/修改套餐")
    public Result updateSetMeal(@RequestBody Setmeal setMeal, @RequestParam("checkGroupIds") Integer[] checkGroupIds){
        try {
            //调用生产者修改数据
            setMealService.updateSetMeal(setMeal,checkGroupIds);
            //修改成功
            return new Result(true, "修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            //修改失败
            return new Result(false,"修改失败");
        }
    }
}
